<?php
namespace application\cms\controller\admin;


class Nav{

    private $rs_nav;
    
    function __construct()
    {
        //查询分类列表 select
        $sql="select * from ".tablename('rj_cms_nav')." order by place,parent_path,list_order ";
        $this->rs_nav=$GLOBALS['db']->query($sql);
    }

    //导航首页
    function index(){   

        //查询导航列表
        $sql="select * from ".tablename('rj_cms_nav');
        if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
            $keyword = trim($_REQUEST['keyword']);
            $sql .= " where name like '%$keyword%' ";
        }
        $sql .= " order by place,parent_path,list_order ";

        //$page=new \library\deep\Page(20);
        $page= \library\deep\Page::new_instance();

        $sql=$page->getPageSQL( $sql , 20 );

        $rs=$GLOBALS['db']->query($sql);
        
        require view('admin/nav/nav.tpl.php');
    } 

    //添加导航页面
    function nav_add(){

        require view('admin/nav/nav_add.tpl.php');
    }

    //修改导航页面
    function nav_edit(){

        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="select * from ".tablename('rj_cms_nav')." where id=".$id;
            $ret=$GLOBALS['db']->query($sql)->fetch();
        }else{
            message('未找到该导航,请刷新页面',url([ _MODULE_NAME_ ,'admin/nav','index']));
        }
        require view('admin/nav/nav_edit.tpl.php');
    }

    //保存
    function nav_save(){
        if(!isset($_POST['name']) && $_POST['name']==''){
            message('导航名称不能为空',url([ _MODULE_NAME_ ,'admin/nav','index']));
        }

        //字段
		$data=[
			'parent_id' => $_POST['parent_id'] ,
			'name' => $_POST['name'] ,
			'nav_img' => isset($_POST['nav_img'])?$_POST['nav_img']:'' ,
			'tip' => $_POST['tip'] ,
			'place' => $_POST['place'] ,
			'url' => $_POST['url'] ,
			'list_order' => $_POST['list_order'] ,
			'status' => $_POST['status'] ,
		];
        
        if( isset($_POST['id']) && $_POST['id']!=''){
            //修改内容
			$where['id']= intval($_POST['id']);

            if($data['parent_id']==0){
                $data['parent_path'] = '0,' . $_POST['id'] . ',';	
            }else{
                $sql="select parent_path from ".tablename('rj_cms_nav')." where id= " . $data['parent_id'];// 父路径
                $data['parent_path'] = $GLOBALS['db']->query($sql)->fetchColumn() ;
                $data['parent_path'] = $data['parent_path']  . $_POST['id'] . ',';
            }

            // 子类父路径批量修改
            $sql="select parent_path from ".tablename('rj_cms_nav')." where id= " . $_POST['id'];// 
            $ret_parent_path = $GLOBALS['db']->query($sql)->fetchColumn();
            $sql="select * from ".tablename('rj_cms_nav')." where parent_path like '$ret_parent_path%' " ;// 
            $rs_parent_path = $GLOBALS['db']->query($sql);
            foreach($rs_parent_path as $row){
                $tmp = str_replace( $ret_parent_path , $data['parent_path'] , $row['parent_path'] );
                pdo_update('rj_cms_nav',['parent_path'=> $tmp],['id'=>$row['id']]);
            }
            //执行sql
            $ok = pdo_update('rj_cms_nav',$data,$where);
            if($ok){
                message('修改成功',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }else{
                message('修改失败',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }
        }else{
            //添加
            $ok = pdo_insert('rj_cms_nav',$data);
            if($ok){
                $id=$GLOBALS['db']->lastInsertId();
                if($data['parent_id']==0){
                    $parent_path = "0,$id,";	
                }else{
                    $sql="select parent_path from ".tablename('rj_cms_nav')." where id= " . $data['parent_id'];// 父路径
                    $parent_path = $GLOBALS['db']->query($sql)->fetchColumn() ;
                    $parent_path = $parent_path . $id . ',';
                }
                pdo_update('rj_cms_nav',['parent_path'=>$parent_path],['id'=>$id]);

                message('添加成功',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }else{
                message('添加失败',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }
        }
    }
    
    //删除导航
    function nav_delete(){
        
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="delete from ".tablename('rj_cms_nav')." where id=".$id;
            $ok=$GLOBALS['db']->exec($sql);
            if($ok){
                message('删除成功',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }else{
                message('删除失败',url([ _MODULE_NAME_ ,'admin/nav','index']));
            }
        }else{
            message('未找到该导航,请刷新页面',url([ _MODULE_NAME_ ,'admin/nav','index']));
        }
    }



    // 弹窗内容 选择 跳转网址
    function nav_url(){   

        $html='';
        //首页
        $html.='<div class="layui-col-xs4" style="text-align:center;padding-top: 12px;">';
        $html.='    <button type="button"  class="layui-btn layui-btn-primary  layui-elip confirm_url" style="width:90%;height:35px;line-height: 35px;" data-url="'.url([_MODULE_NAME_,'index','index']).'">首页</button>';
        $html.='</div>';

        $html.='<br class="layui-clear">';
        $html.='<div style="border-top:1px dotted #aaa;margin:20px 0 -10px;color:#888">文章分类</div>';
        // 文章分类
        $rs_article = $GLOBALS['db']->query('SELECT * FROM ' . tablename('rj_cms_article_sort') . ' where status = 1 ORDER BY list_order ');
        foreach($rs_article as $row){
            $html.='<div class="layui-col-xs4" style="text-align:center;padding-top: 12px;">';
            $html.='    <button type="button"  class="layui-btn layui-btn-primary layui-border-blue  layui-elip confirm_url" style="width:90%;height:35px;line-height: 35px;" data-url="'.url([_MODULE_NAME_,'article','article_list'],['sort_id'=>$row['sort_id']]).'" title="'.$row['sort_name'].'" >';
            $html.='        '.$row['sort_name'].'</button></div>';
        }

        $html.='<br class="layui-clear">';
        $html.='<div style="border-top:1px dotted #aaa;margin:20px 0 -10px;color:#888">商品分类</div>';
        // 商品分类
        $rs_goods = $GLOBALS['db']->query('SELECT * FROM ' . tablename('rj_cms_goods_sort') . ' where status = 1 ORDER BY list_order');
        foreach($rs_goods as $row){
            $html.='<div class="layui-col-xs4" style="text-align:center;padding-top: 12px;">';
            $html.='    <button type="button"  class="layui-btn layui-btn-primary layui-border-orange  layui-elip confirm_url" style="width:90%;height:35px;line-height: 35px;" data-url="'.url([_MODULE_NAME_,'goods','goods_list'],['sort_id'=>$row['sort_id']]).'" title="'.$row['sort_name'].'" >';
            $html.='        '.$row['sort_name'].'</button></div>';
        }

        $html.='<br class="layui-clear">';
        $html.='<div style="border-top:1px dotted #aaa;margin:20px 0 -10px;color:#888">单页</div>';
        // 单页
        $rs_page = $GLOBALS['db']->query("SELECT * FROM " . tablename('rj_cms_page')  ." where status=1 ORDER BY list_order ");
        foreach($rs_page as $row){
            $html.='<div class="layui-col-xs4" style="text-align:center;padding-top: 12px;">';
            $html.='    <button type="button"  class="layui-btn layui-btn-primary layui-border-green  layui-elip confirm_url" style="width:90%;height:35px;line-height: 35px;" data-url="'.url([_MODULE_NAME_,'page','page'],['id'=>$row['id']]).'" title="'.$row['title'].'" >';
            $html.='        '.$row['title'].'</button></div>';
        }
        
        return $html;
    } 


}